<template>
<!--    留言中心-->
    <div>
        <div v-if="flag" style="text-align: center">没有留言</div>
        <div v-else class="box">
            <div class="right1">
                <div v-for="(item,index) in userinfo" :key="index" class="rightbox" @click="change(item.user_id, index)" :style="n == index ? 'background-color: #e9e9e9' : 'background-color: white'">
                    <div class="avatar">
                        <img src="../assets/images/123.jpg" alt="" v-if="item.head_img == '123.jpg'">
                        <img :src="item.head_img" alt="" v-else>
                    </div>
                    <div class="name">
                        <div>{{item.nickname}}</div>
                        <div style="margin-top: 5px;font-size: 12px">{{item.professional}}</div>
                    </div>
                </div>
            </div>
            <div class="left1">
                <div class="leftbox" id="rightMessage">
                    <div v-for="(item,index) in list" :key="index" class="leftbox1">
                        <div v-if="user_id == item.user_id" class="leftboxright">
                            <div>
                                {{item.chat_content}}
                            </div>
                        </div>
                        <div v-else class="leftboxleft">
                            <div>
                                <div class="leftimg">
                                    <img src="../assets/images/123.jpg" alt="" v-if="img == '123.jpg'">
                                    <img :src="img" alt="" v-else>
                                </div>
                                <div class="test">
                                    {{item.chat_content}}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="send">
                    <textarea rows="2" cols="20" v-model="textarea" placeholder="说点儿什么吧" id="myTextarea"></textarea>
                    <div class="sendbox">
                        <el-button type="primary" @click="send">发送</el-button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import {getliuyanren, getothersuser, getliuyan, getuser, liuyanuser} from '../api/api'
    export default {
        name: "message",
        data(){
            return {
                flag:true,
                userinfo:[],
                list:[],
                user_id:'',
                img:'',
                n:0,
                textarea:'',
                source_id: ''
            }
        },
        methods:{
            change(user_id, index){
                this.n = index
                this.source_id = user_id
                getliuyan(user_id).then(res=>{
                    // console.log(res)
                    if(res.status == 0){
                        this.list = res.data
                    }
                })
            },
            send(){
                if(this.textarea.length != 0){
                    liuyanuser(this.source_id, this.textarea).then(res=>{
                        // console.log(res)
                        if(res.status == 0){
                            let a = {
                                user_id:this.user_id,
                                source_id:this.source_id,
                                chat_content:this.textarea
                            }
                            this.list.push(a)
                        }
                        this.textarea = ''
                    })
                }else {
                    this.$message({
                        showClose: true,
                        message: '请输入留言',
                        type: 'info'
                    });
                }

            }
        },
        watch:{
            textarea:function () {
                document.getElementById('myTextarea').scrollTop = document.getElementById('myTextarea').clientHeight
            }
        },
        created() {
            getliuyanren().then(res=>{
                console.log(res)
                if(res.status == 0){
                    if(res.data.length != 0){
                        this.flag = false
                        this.source_id = res.data[0]
                        getliuyan(Number(res.data[0])).then(b=>{
                            console.log(b)
                            if(b.status == 0){
                                this.list = b.data
                            }
                        })
                        getothersuser(Number(res.data[0])).then(d=>{
                            // console.log(d)
                            if(d.status == 0){
                                this.img = d.data[0].head_img
                                // console.log(this.img)
                            }
                        })
                        res.data.forEach(value=>{
                            getothersuser(value).then(a=>{
                                // console.log(a)
                                this.userinfo = this.userinfo.concat(a.data)
                            })
                        })
                    }
                }
            })
            getuser().then(res=>{
                if(res.status == 0){
                    // console.log(res.data.user_id)
                    this.user_id = res.data.user_id
                }
            })
        },
        updated() {
            document.getElementById('rightMessage').scrollTop = document.getElementById('rightMessage').clientHeight
        }
    }
</script>

<style scoped>
    .box {
        display: flex;
    }
    .right1 {
        width: 200px;
        height: 600px;
        background-color: white;
        overflow: auto;
        border-radius: 10px 0px 0px 10px;
        border-right: 1px solid #e9e9e9;
    }
    .rightbox {
        width: 100%;
        padding: 10px;
        box-sizing: border-box;
        display: flex;
        align-items: center;
        cursor: pointer;
        overflow: hidden;
    }
    .rightbox:hover {
        background-color: #e9e9e9;
    }
    .avatar {
        width: 50px;
        height: 50px;
        border-radius: 50%;
        overflow: hidden;
    }
    .avatar>img {
        width: 100%;
        height: 100%;
    }
    .name {
        margin-left: 10px;
    }
    .left1 {
        width: calc(100% - 200px);
        height: 600px;
        border-radius: 0px 10px 10px 0px;
        background-color: white;
        overflow: hidden;
    }
    .leftbox {
        height: 450px;
        background-color: white;
        overflow: auto;
        padding: 30px 20px 50px 20px;
        box-sizing: border-box;
        border-bottom: 1px solid #e9e9e9;
    }
    .leftbox1 {
        overflow: hidden;
    }
    .leftbox1>div {
        margin-top: 20px;
    }
    .leftbox1:first-child>div {
        margin-top: 0px;
    }
   .leftboxright {
       width: 50%;
       float: right;
       display: flex;
       justify-content: flex-end;
   }
    .leftboxright>div {
        padding: 8px;
        display: inline-block;
        text-align: justify;
        word-wrap: break-word;
        max-width: 280px;
        box-sizing: border-box;
        background-color: skyblue;
        color: white;
        border-radius: 10px;
        font-size: 14px;
    }
    .leftboxleft {
        width: 50%;
        float: left;
    }
    .leftboxleft>div {
        display: flex;
    }
    .leftimg {
        width: 40px;
        height: 40px;
        border-radius: 50px;
        margin-right: 10px;
        overflow: hidden;
    }
    .leftimg>img {
        width: 100%;
        height: 100%;
    }
    .test {
        padding: 8px;
        display: inline-block;
        text-align: justify;
        word-wrap: break-word;
        max-width: calc(100% - 50px);
        box-sizing: border-box;
        border-radius: 10px;
        background-color: #e9e9e9;
        font-size: 14px;
    }
    .send {
        position: relative;
    }
    textarea {
        width: 100%;
        height: 150px;
        resize: none;
        text-indent: 1em;
        outline: 0;
        padding: 10px 20px 65px 10px;
        border: 0;
        box-sizing: border-box;
    }
    .sendbox {
        position: absolute;
        bottom: 20px;
        right: 20px;
    }
</style>
